import axios from 'axios'
import { HOST } from './config'
import { Notification } from 'element-ui'
import { getToken,removeToken } from '@/router/cookie'

// 创建axios实例
const service = axios.create({
  baseURL: HOST.url, // api的base_url
  timeout: 5000 //超时时间
})

// // request拦截器
service.interceptors.request.use(config => {

  if (getToken()) {
    config.headers['token'] = getToken() // 让每个请求携带token--['X-Token']为自定义key 请根据实际情况自行修改
  }
  return config
}, error => {
  Promise.reject(error)
})

// respone拦截器
service.interceptors.response.use(
  response => {

    const errcode = response.data.errcode;
    const errmsg = response.data.errmsg;
    if(errcode!==0){
      Notification.error({ title:'错误',message: errmsg })
    }else{
      return response.data;
    }
    
  },
  error => {
    console.log('err' + error)// for debug
    Message.error(res.message)
    return Promise.reject(error)
  }
)


export default service
